package org.wishwork.api.aftersale.upload;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wishwork.api.Api;
import org.wishwork.api.aftersale.upload.request.AfterSaleUploadRequest;
import org.wishwork.api.aftersale.upload.request.Item;
import org.wishwork.context.Context;
import org.wishwork.helper.HttpHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 售后上传
 * @author: WangYX
 * @create: 2022-04-12 16:09
 * @Version: 1.0.0
 **/

public class AfterSaleUploadApi extends Api<List<AfterSaleUploadResponse>, List<AfterSaleUploadRequest>> {

    private static final Logger log = LoggerFactory.getLogger(AfterSaleUploadApi.class);

    private Context context;

    public AfterSaleUploadApi(Context context) {
        this.context = context;
    }

    @Override
    protected String query(List<AfterSaleUploadRequest> afterSaleUploadRequests) throws Exception {
        String url = "/open/aftersale/upload";
        List<Map<String, Object>> list = new ArrayList<>();
        for (AfterSaleUploadRequest afterSaleUploadRequest : afterSaleUploadRequests) {
            Map<String, Object> params = new HashMap<>();
            params.put("shop_id", afterSaleUploadRequest.getShopId());
            params.put("outer_as_id", afterSaleUploadRequest.getOuterAsId());
            params.put("so_id", afterSaleUploadRequest.getSoId());
            params.put("type", afterSaleUploadRequest.getType());
            if (StrUtil.isNotEmpty(afterSaleUploadRequest.getLogisticsCompany())) {
                params.put("logistics_company", afterSaleUploadRequest.getLogisticsCompany());
            }
            if (StrUtil.isNotEmpty(afterSaleUploadRequest.getLId())) {
                params.put("l_id", afterSaleUploadRequest.getLId());
            }
            params.put("shop_status", afterSaleUploadRequest.getShopStatus());
            if (StrUtil.isNotEmpty(afterSaleUploadRequest.getRemark())) {
                params.put("remark", afterSaleUploadRequest.getRemark());
            }
            params.put("good_status", afterSaleUploadRequest.getGoodStatus());
            params.put("question_type", afterSaleUploadRequest.getQuestionType());
            params.put("total_amount", afterSaleUploadRequest.getTotalAmount());
            params.put("refund", afterSaleUploadRequest.getRefund());
            params.put("payment", afterSaleUploadRequest.getPayment());
            List<Map<String, Object>> items = new ArrayList<>();
            for (Item item : afterSaleUploadRequest.getItems()) {
                Map<String, Object> map = new HashMap<>();
                if (StrUtil.isNotEmpty(item.getOuterOiId())) {
                    map.put("outer_oi_id", item.getOuterOiId());
                }
                map.put("sku_id", item.getSkuId());
                map.put("qty", item.getQty());
                map.put("amount", item.getAmount());
                map.put("type", item.getType());
                if (StrUtil.isNotEmpty(item.getName())) {
                    map.put("name", item.getName());
                }
                if (StrUtil.isNotEmpty(item.getPic())) {
                    map.put("pic", item.getPic());
                }
                if (StrUtil.isNotEmpty(item.getPropertiesValue())) {
                    map.put("properties_value", item.getPropertiesValue());
                }
                items.add(map);
            }
            params.put("items", items);
            list.add(params);
        }
        String biz = JSON.toJSONString(list);
        return HttpHelper.sendRequest(biz, url);
    }

    @Override
    protected List<AfterSaleUploadResponse> process(JSONObject data, List<AfterSaleUploadRequest> afterSaleUploadRequests) throws Exception {
        JSONArray datas = data.getJSONArray("datas");
        List<AfterSaleUploadResponse> afterSaleUploadResponses = datas.toJavaList(AfterSaleUploadResponse.class);
        return afterSaleUploadResponses;
    }
}
